# -*- coding: utf-8 -*- 
#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################

db.define_table('projects',
                Field('name', 'string', unique = True, label = 'Project Name'),
                Field('short_description', 'string',
                      label = 'Short Description'),
                Field('description', 'text', label = 'Description'),
                Field('creation_date', 'datetime', label = 'Creation Date',
                      writable = False, readable = False,
                      default = request.now),
                Field('created_by', db.auth_user, default = auth.user_id,
                      writable = False, readable = False),
                Field('modified_by', db.auth_user, default = auth.user_id,
                      update = auth.user_id, writable = False,
                      readable = False),
                format = '%(name)s')

db.define_table('users',
                Field('name', db.auth_user, label = 'Name'),
                Field('project', db.projects, label = 'Project Name'),
                Field('created_by', db.auth_user, default = auth.user_id,
                      writable = False, readable = False),
                Field('modified_by', db.auth_user, default = auth.user_id,
                      update = auth.user_id, writable = False,
                      readable = False),
                format = '%(name)s')

db.define_table('todos',
                Field('title', 'string', unique = True, label = 'Title'),
                Field('description', 'text', label = 'Description'),
                Field('completion_date', 'datetime', label = 'Completion Date'),
                Field('status', 'string', label = 'Todo Status',
                      default='Not Started'),
                Field('assignee', db.auth_user, label = 'Owner',
                      default = auth.user_id),
                Field('project', db.projects, label = 'Project Name'),
                Field('email_sent', 'boolean', default = False,
                      writable = False, readable = False),
                Field('created_by', db.auth_user, default = auth.user_id,
                      writable = False, readable = False),
                Field('modified_by', db.auth_user, default = auth.user_id,
                      update = auth.user_id, writable = False,
                      readable = False),
                format = '%(title)s')

db.define_table('todofeedback',
                Field('todo', db.todos, label = 'Todo Item'),
                Field('comment', 'text', label = 'Comments'),
                Field('parent_comment', 'reference todofeedback',
                      writable = False, readable = False),
                Field('time_posted2','datetime',default=request.now,label = 'Time Posted',
                      writable=False,readable=True),
                Field('created_by', db.auth_user, default = auth.user_id,
                      writable = False, readable = False),
                Field('modified_by', db.auth_user, default = auth.user_id,
                      update = auth.user_id, writable = False,
                      readable = False),
                format = '%(comment)s')

db.define_table('sitefeedback',
                Field('comment', 'text', label = 'Comments'),
                Field('created_by', db.auth_user, default = auth.user_id,
                      writable = False, readable = False),
                Field('modified_by', db.auth_user, default = auth.user_id,
                      update = auth.user_id, writable = False,
                      readable = False))

db.projects.name.requires = [IS_NOT_EMPTY(
                             error_message = T('Project Name is required.')),
                             IS_NOT_IN_DB(db, 'projects.name',
                             error_message = 'Project Name is already in use.')]
db.projects.short_description.requires = IS_NOT_EMPTY(
                           error_message = T('Short Descrpition is required.'))
db.projects.description.requires = IS_NOT_EMPTY(
                                 error_message = T('Descrpition is required.'))
db.projects.creation_date.requires = IS_NOT_EMPTY()
db.projects.created_by.requires = IS_IN_DB(db, 'auth_user.id', '%(username)s')
db.projects.modified_by.requires = IS_IN_DB(db, 'auth_user.id', '%(username)s')

db.todos.title.requires = [IS_NOT_EMPTY(
                           error_message = T('Title is required.')),
                           IS_NOT_IN_DB(db, 'todos.title',
                           error_message = 'Title is already in use.')]
db.todos.description.requires = IS_NOT_EMPTY(
                           error_message = T('Short Descrpition is required.'))
db.todos.completion_date.requires = IS_NOT_EMPTY(
                             error_message = T('Completion Date is required.'))
db.todos.status.requires = IS_IN_SET(('Completed','In Progress','Blocked','Not Started'),
                                     zero=T(''),
                                     error_message = T('Status is required.'))
db.todos.assignee.requires = IS_IN_DB(db, 'auth_user.id', '%(username)s',
                                      error_message = T('Owner is required.'))
db.todos.project.requires = IS_IN_DB(db, 'projects.id', '%(name)s',
                                     zero=T('choose one'),
                                     error_message = T('Project is required.'))
db.todos.created_by.requires = IS_IN_DB(db, 'auth_user.id', '%(username)s')
db.todos.modified_by.requires = IS_IN_DB(db, 'auth_user.id', '%(username)s')

db.todofeedback.comment.requires = IS_NOT_EMPTY(
                                error_message = T('Comment can not be blank.'))
#db.todofeedback.time_posted.requires = IS_NOT_EMPTY(
#                                error_message = T('Time is reqired.'))
db.todofeedback.created_by.requires = IS_IN_DB(db, 'auth_user.id',
                                               '%(username)s')
db.todofeedback.modified_by.requires = IS_IN_DB(db, 'auth_user.id',
                                                '%(username)s')

db.sitefeedback.comment.requires = IS_NOT_EMPTY(
                                error_message = T('Comment can not be blank.'))
db.sitefeedback.created_by.requires = IS_IN_DB(db, 'auth_user.id',
                                               '%(username)s')
db.sitefeedback.modified_by.requires = IS_IN_DB(db, 'auth_user.id',
                                                '%(username)s')
